package data.input;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;

import org.joda.time.LocalDate;

import data.enums.Index;
import data.transform.DateTransform;
import entities.IndexTuple;

public class YahooCsv extends Data implements Serializable {

	private static final long serialVersionUID = 4300169987338264859L;

	/**
	 * Should not be instantiated with operator "new". Use method "createData" instead
	 * 
	 * @throws IOException
	 * @throws ParseException
	 */
	protected YahooCsv(Index indexName) throws IOException, ParseException {
		initializeTuplesMap(indexName);
	}
	

	@Override
	protected void initializeTuplesMapWithoutClassification(Index indexName) throws FileNotFoundException, IOException {
		
		@SuppressWarnings("resource")
		BufferedReader reader = new BufferedReader (new FileReader(indexName.toString() + ".csv"));

		// first line does not contain data
		String line = reader.readLine();

		while ((line = reader.readLine()) != null) {

			String[] data = line.split(",");

			LocalDate date = DateTransform.yahooCsvDateToLocalDate(data[0]);
			BigDecimal close = new BigDecimal(data[4]);

			tuplesMap.put(date, new IndexTuple(date, close));

		}
	}


}
